package com.bskj.sj.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.enums.UserStatusEnum;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.StringUtils;
import com.bskj.sj.service.IBmsUserService;
import com.bskj.sj.service.IParkingShopService;

public class SessionFilter implements MyFilter {
	private MyFilter next;

	private Set<String> prefixIgnores = new HashSet<String>();

	private IBmsUserService bmsUserService;

	private IParkingShopService parkingShopService;

	public IBmsUserService getBmsUserService() {
		return bmsUserService;
	}

	public void setBmsUserService(IBmsUserService bmsUserService) {
		this.bmsUserService = bmsUserService;
	}

	public IParkingShopService getParkingShopService() {
		return parkingShopService;
	}

	public void setParkingShopService(IParkingShopService parkingShopService) {
		this.parkingShopService = parkingShopService;
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		if (canIgnore(request)) {
			if (next != null) {
				next.doFilter(request, response, chain);
			} else {
				chain.doFilter(request, response);
			}
			return;
		}

		LogUtils.debug("url:" + request.getRequestURI());
		Map<String, Object> extraParams = new HashMap<String, Object>();
		LogUtils.debug(JSONObject.toJSONString(request.getParameterMap()));
		String custIdStr = request.getParameter("custId");
		if (StringUtils.isBank(custIdStr)) {
			try {
				BmsUser bmsUser = bmsUserService.findUser(request.getParameter("appId"),
						request.getParameter("openId"));
				LogUtils.debug(JSONObject.toJSONString(bmsUser));
				
				if (bmsUser != null) {
					if(bmsUser.getShopId()!=null){
						ParkingShop parkingShop=bmsUserService.findShop(bmsUser.getShopId());
						if(parkingShop != null && parkingShop.getStatus()!=1){
							request.getRequestDispatcher("/authInfo/page/block").forward(request, response);
							return;
						}
					}
					if(bmsUser.getStatus()==UserStatusEnum.disable.getKey()){
						request.getRequestDispatcher("/authInfo/page/block").forward(request, response);
						return;
					}
					Integer custId = bmsUser.getCustId();
					Integer shopId = bmsUser.getShopId();
					extraParams.put("authId", bmsUser.getId());
					extraParams.put("type", bmsUser.getType());
					extraParams.put("custId", custId);
					extraParams.put("shopId", shopId);
					RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request, extraParams);
					if (next != null) {
						next.doFilter(requestParameterWrapper, response, chain);
					} else {
						chain.doFilter(requestParameterWrapper, response);
					}
					return;
				} else {
					request.getRequestDispatcher("/authInfo/page/bindWx").forward(request, response);
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		chain.doFilter(request, response);
	}

	public void destroy() {
		prefixIgnores = null;
	}

	private boolean canIgnore(HttpServletRequest request) {
		String url = request.getParameter("uri");
		if (StringUtils.isBank(url)) {
			url = request.getRequestURI();
		}
		String contextPath = request.getContextPath();
		for (String ignore : prefixIgnores) {
			if (url.startsWith(contextPath + ignore)) {
				return true;
			}
			if (url.startsWith(ignore)) {
				return true;
			}
		}
		return false;
	}

	public Set<String> getPrefixIgnores() {
		return prefixIgnores;
	}

	public void setPrefixIgnores(Set<String> prefixIgnores) {
		this.prefixIgnores = prefixIgnores;
	}

	public MyFilter next() {
		return next;
	}

	public void setNext(MyFilter myFitler) {
		this.next = myFitler;
	}
}
